package com.evan.review.architecture;

import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

/**
 * 架构模式对比Activity
 * 展示不同架构模式的优缺点和适用场景
 */
public class ArchitectureComparisonActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_architecture_comparison);
        
        // 初始化表格
        initComparisonTable();
    }
    
    private void initComparisonTable() {
        TableLayout tableLayout = findViewById(R.id.table_comparison);
        
        // 添加MVC行
        addArchitectureRow(tableLayout, 
                "MVC",
                "简单直观，容易理解和实现",
                "Controller和View耦合度高，难以单元测试，UI逻辑复杂时代码难以维护",
                "适用于简单的UI界面，快速开发小型应用");
        
        // 添加MVP行
        addArchitectureRow(tableLayout, 
                "MVP",
                "View和Presenter通过接口解耦，便于单元测试，UI逻辑清晰",
                "需要编写大量接口，代码量增加，Presenter容易变得臃肿",
                "适用于UI交互复杂的应用，需要高度可测试性的场景");
        
        // 添加MVVM行
        addArchitectureRow(tableLayout, 
                "MVVM",
                "数据绑定减少样板代码，ViewModel与View完全解耦，支持响应式编程",
                "数据绑定调试困难，初学者学习曲线陡峭，过度使用可能导致性能问题",
                "适用于数据驱动的应用，需要实时更新UI的场景");
        
        // 添加MVI行
        addArchitectureRow(tableLayout, 
                "MVI",
                "单向数据流使状态可预测，不可变状态避免副作用，便于调试和测试",
                "概念抽象，学习曲线陡峭，简单功能实现也需要编写大量代码",
                "适用于状态复杂且需要严格管理的应用，如金融类应用");
    }
    
    private void addArchitectureRow(TableLayout tableLayout, String pattern, String pros, String cons, String useCases) {
        // 添加架构模式名称
        TableRow patternRow = new TableRow(this);
        TextView patternLabel = createHeaderTextView("架构模式");
        TextView patternValue = createValueTextView(pattern);
        patternRow.addView(patternLabel);
        patternRow.addView(patternValue);
        tableLayout.addView(patternRow);
        
        // 添加优点
        TableRow prosRow = new TableRow(this);
        TextView prosLabel = createHeaderTextView("优点");
        TextView prosValue = createValueTextView(pros);
        prosRow.addView(prosLabel);
        prosRow.addView(prosValue);
        tableLayout.addView(prosRow);
        
        // 添加缺点
        TableRow consRow = new TableRow(this);
        TextView consLabel = createHeaderTextView("缺点");
        TextView consValue = createValueTextView(cons);
        consRow.addView(consLabel);
        consRow.addView(consValue);
        tableLayout.addView(consRow);
        
        // 添加适用场景
        TableRow useCasesRow = new TableRow(this);
        TextView useCasesLabel = createHeaderTextView("适用场景");
        TextView useCasesValue = createValueTextView(useCases);
        useCasesRow.addView(useCasesLabel);
        useCasesRow.addView(useCasesValue);
        tableLayout.addView(useCasesRow);
        
        // 添加分隔行
        TableRow separatorRow = new TableRow(this);
        TextView separator = new TextView(this);
        separator.setHeight(2);
        separator.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
        separator.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 2));
        separatorRow.addView(separator);
        tableLayout.addView(separatorRow);
    }
    
    private TextView createHeaderTextView(String text) {
        TextView textView = new TextView(this);
        textView.setText(text);
        textView.setPadding(16, 16, 16, 16);
        textView.setTextColor(getResources().getColor(android.R.color.black));
        textView.setTextSize(14);
        textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        return textView;
    }
    
    private TextView createValueTextView(String text) {
        TextView textView = new TextView(this);
        textView.setText(text);
        textView.setPadding(16, 16, 16, 16);
        textView.setTextColor(getResources().getColor(android.R.color.darker_gray));
        textView.setTextSize(14);
        textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1));
        return textView;
    }
} 